<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.

(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual.  Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Searching Memory</title>

<meta name="description" content="Debugging with GDB: Searching Memory">
<meta name="keywords" content="Debugging with GDB: Searching Memory">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Data.html#Data" rel="up" title="Data">
<link href="Value-Sizes.html#Value-Sizes" rel="next" title="Value Sizes">
<link href="Caching-Target-Data.html#Caching-Target-Data" rel="prev" title="Caching Target Data">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Searching-Memory"></a>
<div class="header">
<p>
Next: <a href="Value-Sizes.html#Value-Sizes" accesskey="n" rel="next">Value Sizes</a>, Previous: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="p" rel="prev">Caching Target Data</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Search-Memory"></a>
<h3 class="section">10.22 Search Memory</h3>
<a name="index-searching-memory"></a>

<p>Memory can be searched for a particular sequence of bytes with the
<code>find</code> command.
</p>
<dl compact="compact">
<dd><a name="index-find"></a>
</dd>
<dt><code>find <span class="roman">[</span>/<var>sn</var><span class="roman">]</span> <var>start_addr</var>, +<var>len</var>, <var>val1</var> <span class="roman">[</span>, <var>val2</var>, &hellip;<span class="roman">]</span></code></dt>
<dt><code>find <span class="roman">[</span>/<var>sn</var><span class="roman">]</span> <var>start_addr</var>, <var>end_addr</var>, <var>val1</var> <span class="roman">[</span>, <var>val2</var>, &hellip;<span class="roman">]</span></code></dt>
<dd><p>Search memory for the sequence of bytes specified by <var>val1</var>, <var>val2</var>,
etc.  The search begins at address <var>start_addr</var> and continues for either
<var>len</var> bytes or through to <var>end_addr</var> inclusive.
</p></dd>
</dl>

<p><var>s</var> and <var>n</var> are optional parameters.
They may be specified in either order, apart or together.
</p>
<dl compact="compact">
<dt><span class="roman"><var>s</var>, search query size</span></dt>
<dd><p>The size of each search query value.
</p>
<dl compact="compact">
<dt><code>b</code></dt>
<dd><p>bytes
</p></dd>
<dt><code>h</code></dt>
<dd><p>halfwords (two bytes)
</p></dd>
<dt><code>w</code></dt>
<dd><p>words (four bytes)
</p></dd>
<dt><code>g</code></dt>
<dd><p>giant words (eight bytes)
</p></dd>
</dl>

<p>All values are interpreted in the current language.
This means, for example, that if the current source language is C/C<tt>++</tt>
then searching for the string &ldquo;hello&rdquo; includes the trailing &rsquo;\0&rsquo;.
</p>
<p>If the value size is not specified, it is taken from the
value&rsquo;s type in the current language.
This is useful when one wants to specify the search
pattern as a mixture of types.
Note that this means, for example, that in the case of C-like languages
a search for an untyped 0x42 will search for &lsquo;<samp>(int) 0x42</samp>&rsquo;
which is typically four bytes.
</p>
</dd>
<dt><span class="roman"><var>n</var>, maximum number of finds</span></dt>
<dd><p>The maximum number of matches to print.  The default is to print all finds.
</p></dd>
</dl>

<p>You can use strings as search values.  Quote them with double-quotes
 (<code>&quot;</code>).
The string value is copied into the search pattern byte by byte,
regardless of the endianness of the target and the size specification.
</p>
<p>The address of each match found is printed as well as a count of the
number of matches found.
</p>
<p>The address of the last value found is stored in convenience variable
&lsquo;<samp>$_</samp>&rsquo;.
A count of the number of matches is stored in &lsquo;<samp>$numfound</samp>&rsquo;.
</p>
<p>For example, if stopped at the <code>printf</code> in this function:
</p>
<div class="smallexample">
<pre class="smallexample">void
hello ()
{
  static char hello[] = &quot;hello-hello&quot;;
  static struct { char c; short s; int i; }
    __attribute__ ((packed)) mixed
    = { 'c', 0x1234, 0x87654321 };
  printf (&quot;%s\n&quot;, hello);
}
</pre></div>

<p>you get during debugging:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) find &amp;hello[0], +sizeof(hello), &quot;hello&quot;
0x804956d &lt;hello.1620+6&gt;
1 pattern found
(gdb) find &amp;hello[0], +sizeof(hello), 'h', 'e', 'l', 'l', 'o'
0x8049567 &lt;hello.1620&gt;
0x804956d &lt;hello.1620+6&gt;
2 patterns found
(gdb) find /b1 &amp;hello[0], +sizeof(hello), 'h', 0x65, 'l'
0x8049567 &lt;hello.1620&gt;
1 pattern found
(gdb) find &amp;mixed, +sizeof(mixed), (char) 'c', (short) 0x1234, (int) 0x87654321
0x8049560 &lt;mixed.1625&gt;
1 pattern found
(gdb) print $numfound
$1 = 1
(gdb) print $_
$2 = (void *) 0x8049560
</pre></div>

<hr>
<div class="header">
<p>
Next: <a href="Value-Sizes.html#Value-Sizes" accesskey="n" rel="next">Value Sizes</a>, Previous: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="p" rel="prev">Caching Target Data</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
